package com.we.utils;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author ：Jason
 * @date ：Created in 2023/5/30 4:10 PM
 * @description：
 * @modified By：
 * @version: 1.0
 */
public class HttpUtils {
    private static final Logger LOG = LoggerFactory.getLogger(HttpUtils.class);

    public static String httpGet(String url) throws IOException {
        OkHttpClient httpClient =
                new OkHttpClient()
                        .newBuilder()
                        .connectTimeout(60, TimeUnit.SECONDS)
                        .readTimeout(60, TimeUnit.SECONDS)
                        .retryOnConnectionFailure(true)
                        .build();
        Request request = new Request.Builder().url(url).get().build();
        Response response = httpClient.newCall(request).execute();
        String result = response.body().string();
        if (response.isSuccessful()) {
            LOG.info("http get request successful, url is: {}", url);
            return result;
        } else {
            LOG.info("http get request failed, url is {}", url);
            return null;
        }
    }
}
